package com.example.bookmanagersystem.service;

import com.example.bookmanagersystem.entity.Book;
import com.example.bookmanagersystem.repository.BookRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class BookService {
    @Autowired
    private BookRepository bookRepository;

    // 获取所有图书
    public List<Book> getAllBooks() {
        return bookRepository.findAll();
    }

    // 根据ID获取图书
    public Optional<Book> getBookById(Long id) {
        return bookRepository.findById(id);
    }

    // 添加图书
    public Book addBook(Book book) {
        return bookRepository.save(book);
    }

    // 更新图书
    public Book updateBook(Book book) {
        return bookRepository.save(book);
    }

    // 删除图书
    public void deleteBook(Long id) {
        bookRepository.deleteById(id);
    }

    // 根据书名搜索
    public List<Book> searchBooksByTitle(String title) {
        return bookRepository.findByTitleContaining(title);
    }

    // 根据作者搜索
    public List<Book> searchBooksByAuthor(String author) {
        return bookRepository.findByAuthorContaining(author);
    }

    // 获取可借阅的图书
    public List<Book> getAvailableBooks() {
        return bookRepository.findAvailableBooks();
    }
}